package com.phil.xml.parse;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.phi.tuangou.bean.LashouDetailBean;

public class LashouCityDetailHandler extends DefaultHandler {

	List<LashouDetailBean> detailList;
	String elementName = null;
	LashouDetailBean bean = null;
	StringBuffer buf;
	String preTag = null;
	String cityId = null;
	String cityName = null;

	public LashouCityDetailHandler() {
		detailList = new ArrayList<LashouDetailBean>();
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		super.characters(ch, start, length);
		if (preTag != null) {
			// if (elementName.equals("cityid")) {
			// String cityid = new String(ch, start, length);
			// cityid = cityid.trim();
			// System.out.println("cityid  == " + cityid);
			// bean.setCityid(cityid);
			// } else if (elementName.equals("cityname")) {
			// String cityname = new String(ch, start, length);
			// cityname = cityname.trim();
			// bean.setCityname(cityname);
			// } else
			buf = new StringBuffer();
			if (preTag.equals("cityid")) {
				String cityid = new String(ch, start, length);
				buf.append(cityid);
			} else if (preTag.equals("cityname")) {
				String cityname = new String(ch, start, length);
				buf.append(cityname);
			} else if (preTag.equals("goods_title")) {
				String goods_title = new String(ch, start, length);

				buf.append(goods_title);
				// bean.setGoods_title(goods_title);
			} else if (preTag.equals("goods_rules")) {
				String goods_rules = new String(ch, start, length);

				buf.append(goods_rules);
				// bean.setGoods_rules(goods_rules);
			} else if (preTag.equals("goods_class")) {
				String goods_class = new String(ch, start, length);

				buf.append(goods_class);
				// bean.setGoods_class(goods_class);
			} else if (preTag.equals("goods_value")) {
				String goods_value = new String(ch, start, length);

				buf.append(goods_value);
				System.out.println("goodsvalue ******== " + goods_value);
				// bean.setGoods_value(Double.parseDouble(goods_value));
			} else if (preTag.equals("goods_price")) {
				String goods_price = new String(ch, start, length);

				buf.append(goods_price);
				// bean.setGoods_price(Double.parseDouble(goods_price));
			} else if (preTag.equals("goods_rebate")) {
				String goods_rebate = new String(ch, start, length);

				buf.append(goods_rebate);
				// bean.setGoods_rebate(Double.parseDouble(goods_rebate));
			} else if (preTag.equals("goods_start_time")) {
				String goods_start_time = new String(ch, start, length);

				buf.append(goods_start_time);
				// bean.setGoods_start_time(Long.parseLong(goods_start_time));
			} else if (preTag.equals("goods_deadline")) {
				String goods_deadline = new String(ch, start, length);

				buf.append(goods_deadline);
				// bean.setGoods_deadline(Long.parseLong(goods_deadline));
			} else if (preTag.equals("goods_left_second")) {
				String goods_left_second = new String(ch, start, length);

				buf.append(goods_left_second);
				// bean.setGoods_left_second(Long.parseLong(goods_left_second));
			} else if (preTag.equals("goods_expire")) {
				String goods_expire = new String(ch, start, length);

				buf.append(goods_expire);
				// bean.setGoods_expire(Long.parseLong(goods_expire));
			} else if (preTag.equals("goods_convey_fee")) {
				String goods_convey_fee = new String(ch, start, length);

				buf.append(goods_convey_fee);
				System.out.println("chacter good_covery_fee = "
						+ goods_convey_fee);
				// bean.setGoods_convey_fee(Double.parseDouble(goods_convey_fee));
			} else if (preTag.equals("goods_description")) {
				String goods_description = new String(ch, start, length);

				buf.append(goods_description);
				// bean.setGoods_description(goods_description);
			} else if (preTag.equals("goods_bought")) {
				String goods_bought = new String(ch, start, length);

				buf.append(goods_bought);
				// bean.setGoods_bought(Long.parseLong(goods_bought));
			} else if (preTag.equals("goods_min_bought")) {
				String goods_min_bought = new String(ch, start, length);

				buf.append(goods_min_bought);
				// bean.setGoods_min_bought(Long.parseLong(goods_min_bought));
			} else if (preTag.equals("goods_max_bought")) {
				String goods_max_bought = new String(ch, start, length);

				buf.append(goods_max_bought);
				// bean.setGoods_max_bought(Long.parseLong(goods_max_bought));
			} else if (preTag.equals("goods_sp_name")) {
				String goods_sp_name = new String(ch, start, length);

				buf.append(goods_sp_name);
				// bean.setGoods_sp_name(goods_sp_name);
			} else if (preTag.equals("goods_sp_url")) {
				String goods_sp_url = new String(ch, start, length);

				buf.append(goods_sp_url);
				// bean.setGoods_sp_url(goods_sp_url);
			} else if (preTag.equals("goods_image_url")) {
				String goods_image_url = new String(ch, start, length);

				buf.append(goods_image_url);
				// bean.setGoods_image_url(goods_image_url);
			} else if (preTag.equals("goods_site_url")) {
				String goods_site_url = new String(ch, start, length);

				buf.append(goods_site_url);
				// bean.setGoods_site_url(goods_site_url);
			} else if (preTag.equals("goods_phone")) {
				String goods_phone = new String(ch, start, length);

				buf.append(goods_phone);
				// bean.setGoods_phone(goods_phone);
				System.out.println("start elements goods_phone = "
						+ goods_phone);
			}
		}
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub
		super.startElement(uri, localName, qName, attributes);
		System.out.println("start elements qName = " + qName);
		if (qName.equals("goods")) {
			bean = new LashouDetailBean();
			bean.setCityid(cityId);
			bean.setCityname(cityName);
			detailList.add(bean);
		}
		preTag = qName;
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		super.endElement(uri, localName, qName);
		if (preTag != null) {
			if (preTag.equals("cityid")) {
				cityId = buf.toString();
				
			} else if (preTag.equals("cityname")) {
				cityName = buf.toString();
			} else if (qName.equals("goods_title")) {
				String goods_title = buf.toString();
				goods_title = goods_title.trim();
				buf.append(goods_title);
				bean.setGoods_title(goods_title);
				// buf = null;
			} else if (qName.equals("goods_rules")) {
				String goods_rules = buf.toString();
				goods_rules = goods_rules.trim();
				bean.setGoods_rules(goods_rules);
				// buf = null;
			} else if (qName.equals("goods_class")) {
				String goods_class = buf.toString();
				goods_class = goods_class.trim();
				bean.setGoods_class(goods_class);
				// buf = null;
			} else if (qName.equals("goods_value")) {
				String goods_value = buf.toString();
				goods_value = goods_value.trim();
				bean.setGoods_value(Double.parseDouble(goods_value));
				// buf = null;
			} else if (qName.equals("goods_price")) {
				String goods_price = buf.toString();
				goods_price = goods_price.trim();
				bean.setGoods_price(Double.parseDouble(goods_price));
				// buf = null;
			} else if (qName.equals("goods_rebate")) {
				String goods_rebate = buf.toString();
				goods_rebate = goods_rebate.trim();
				bean.setGoods_rebate(Double.parseDouble(goods_rebate));
			} else if (qName.equals("goods_start_time")) {
				String goods_start_time = buf.toString();
				goods_start_time = goods_start_time.trim();
				bean.setGoods_start_time(Long.parseLong(goods_start_time));
				// buf = null;
			} else if (qName.equals("goods_deadline")) {
				String goods_deadline = buf.toString();
				goods_deadline = goods_deadline.trim();
				bean.setGoods_deadline(Long.parseLong(goods_deadline));
				// buf = null;
			} else if (qName.equals("goods_left_second")) {
				String goods_left_second = buf.toString();
				goods_left_second = goods_left_second.trim();
				bean.setGoods_left_second(Long.parseLong(goods_left_second));
				// buf = null;
			} else if (qName.equals("goods_expire")) {
				String goods_expire = buf.toString();
				goods_expire = goods_expire.trim();
				bean.setGoods_expire(Long.parseLong(goods_expire));
				// buf = null;
			} else if (qName.equals("goods_convey_fee")) {
				String goods_convey_fee = buf.toString();
				goods_convey_fee = goods_convey_fee.trim();
				bean.setGoods_convey_fee(Double.parseDouble(goods_convey_fee));
				// buf = null;
			} else if (qName.equals("goods_description")) {
				String goods_description = buf.toString();
				goods_description = goods_description.trim();
				bean.setGoods_description(goods_description);
				// buf = null;
			} else if (qName.equals("goods_bought")) {
				String goods_bought = buf.toString();
				goods_bought = goods_bought.trim();
				bean.setGoods_bought(Long.parseLong(goods_bought));
				// buf = null;
			} else if (qName.equals("goods_min_bought")) {
				String goods_min_bought = buf.toString();
				goods_min_bought = goods_min_bought.trim();
				bean.setGoods_min_bought(Long.parseLong(goods_min_bought));
				// buf = null;
			} else if (qName.equals("goods_max_bought")) {
				String goods_max_bought = buf.toString();
				goods_max_bought = goods_max_bought.trim();
				bean.setGoods_max_bought(Long.parseLong(goods_max_bought));
				// buf = null;
			} else if (qName.equals("goods_sp_name")) {
				String goods_sp_name = buf.toString();
				goods_sp_name = goods_sp_name.trim();
				bean.setGoods_sp_name(goods_sp_name);
				// buf = null;
			} else if (qName.equals("goods_sp_url")) {
				String goods_sp_url = buf.toString();
				goods_sp_url = goods_sp_url.trim();
				bean.setGoods_sp_url(goods_sp_url);
				// buf = null;
			} else if (qName.equals("goods_image_url")) {
				String goods_image_url = buf.toString();
				goods_image_url = goods_image_url.trim();
				bean.setGoods_image_url(goods_image_url);
				// buf = null;
			} else if (qName.equals("goods_site_url")) {
				String goods_site_url = buf.toString();
				goods_site_url = goods_site_url.trim();
				bean.setGoods_site_url(goods_site_url);
				// buf = null;
			} else if (qName.equals("goods_phone")) {
				System.out.println("goods_phone  == " + buf);
				String goods_phone = buf.toString();
				goods_phone = goods_phone.trim();
				bean.setGoods_phone(goods_phone);
				// buf = null;
			}
			preTag = null;
		}
	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		System.out.println("*******begin parse docments*******");
	}

	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("*******end parse docments*******");
	}

	public List<LashouDetailBean> getList() {
		System.out.println("detailList = " + detailList + " size  = "
				+ detailList.size());
		return detailList;
	}
}
